package com.radiant.data.entity;
import java.util.Date;
import java.util.StringTokenizer;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;

import com.radiant.data.annotation.Persist;

/**
 * An abstract class that represents "like" in the system 
 * See {@link PostLike} and {@link CommentLike}
 * @author karthikk
 *
 */
@XmlRootElement
public abstract class Like extends SimpleDBCompoundType implements ActivityFeedable {
	
	private Date timeCreated;

	private String from;
	
	public Date getTimeCreated() {
		return timeCreated;
	}
	
	public void setTimeCreated(Date timeCreated) {
		this.timeCreated = timeCreated;
	}
	
	public String getFrom() {
		return from;
	}
	
	public void setFrom(String from) {
		this.from = from;
	}

	public void initFromString(String likeString) {
		StringTokenizer st = new StringTokenizer(likeString, ":");
		String dateString = st.nextToken();
		String from = st.nextToken();
		String parent = st.nextToken();
		this.setTimeCreated((Date)getBeanUtilsBean().getConvertUtils().convert(dateString, Date.class));
		this.setFrom(from);		
		this.setParentFromString(parent);
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(getBeanUtilsBean().getConvertUtils().convert(getTimeCreated()));
		sb.append(":").append(getFrom()).append(":").append(getParentAsString());		
		return sb.toString();
	}

	protected abstract void setParentFromString(String parent);
	
	protected abstract String getParentAsString();

	public static class LikeConverter implements Converter {

		public Object convert(Class clazz, Object likeObject) {
			Like like = null;
			if (clazz == CommentLike.class) {
				like = new CommentLike();
			} else if (clazz == PostLike.class) {
				like = new PostLike();
			}
			if (like != null) {
				like.initFromString((String)likeObject);
				return like;
			} else if (clazz == String.class) {
				like = (Like) likeObject;
				return like.toString();
			} else {
				return likeObject;
			}
		}
		
	}

	@Override
	protected void registerConverters(ConvertUtilsBean convertUtilsBean) {
		// TODO Auto-generated method stub
		
	}

	public abstract void addToActivity(Activity activity);

	public abstract void addMessageToActivity(Activity activity, User creator, Child child);

}
